//
//  ShareViewController.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2020/5/18.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

class ShareViewController: BaseViewController {
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, kInvWinWidth, kInvWinHeight))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
    }
    
    func setupUI() {
        
        self.view.addSubview(contView)
        contView.snp.makeConstraints { (make) in
            make.width.equalTo(360)
            make.centerX.equalToSuperview()
            make.top.equalTo(20)
            make.bottom.equalTo(-50)
        }
        
        
        self.view.addSubview(copyBtn)
        copyBtn.snp.makeConstraints { (make) in
            make.width.equalTo(56)
            make.height.equalTo(32)
            make.right.equalTo(-20)
            make.bottom.equalTo(-20)
        }
    }
    
    func text() -> String {
        guard let ownid = UserInfoManager.shared.ownid else {
            return "ownid=nil"
        }
        let name = UserInfoManager.shared.disname ?? ""
        let activity = UserInfoManager.shared.activity_name ?? "无"
        let password = UserInfoManager.shared.kPassword ?? "无"
        let newpwd = password=="" ? "无":password
        let ordertime = UserInfoManager.shared.order_time ?? "无"
        /*
        let otime = (ordertime==0 || ordertime==nil) ? Double(Date().timeIntervalSince1970):ordertime!
        let date = Date.init(timeIntervalSince1970: TimeInterval(otime))
        let dFormatter = DateFormatter.init()
        dFormatter.dateFormat = "yyyy年MM月dd日 HH:mm"
        let time = dFormatter.string(from: date)
        */
        
        
        let cont = name+"邀请您参加视频会议".localized()+"\n"
        let cont1 = "会议主题:".localized()+activity+"\n"
        let cont2 = "会议时间:".localized()+ordertime+"\n"
        let cont3 = "会议号码:".localized()+ownid+"\n"
        let cont4 = "会议密码:".localized()+newpwd+"\n\n"
        let link1 = "若已安装壹会通".localized()+"\n"
        let link2 = mt_url_web+ownid
       
        let tex = cont+cont1+cont2+cont3+cont4+link1+link2
        return tex
    }
    
    func loadData() {
        let style = NSMutableParagraphStyle()
        style.lineSpacing = 5
        
        
        let attributedString = NSMutableAttributedString.init(string: self.text())
        attributedString.addAttributes([NSAttributedString.Key.font
            : NSFont.systemFont(ofSize: 14) as Any], range: NSMakeRange(0, attributedString.length))
            attributedString.addAttributes([NSAttributedString.Key.paragraphStyle
                : style as Any], range: NSMakeRange(0, attributedString.length))
        self.contView.textStorage?.setAttributedString(attributedString)
        
        
        /*
        let string = "联系苹果公司寻求支持后"
        //self.contView.string = string
        self.contView.shouldChangeText(in:self.contView.rangeForUserTextChange, replacementString: string)
        self.contView.textStorage?.replaceCharacters(in: self.contView.rangeForUserTextChange, with: string)
        self.contView.didChangeText()
        */
        
    }
    lazy var copyBtn: ZHButton = {
        let btn = ZHButton()
        btn.zh_font = NSFont.systemFont(ofSize: 14)
        btn.zh_title = "复制".localized()
        btn.zh_backgroundColor = color_main140
        btn.zh_color = color_gray(255)
        btn.hasBorder = true
        btn.zh_cornerRadius = 5
        btn.target = self
        btn.action = #selector(copyBtnAction(senber:))
        return btn
    }()
    
    
    
    lazy var contView: NSTextView = {
        let view = NSTextView.init()
        view.wantsLayer = true
        view.delegate = self
        view.isVerticallyResizable = true
        view.isHorizontallyResizable = true
        view.textContainer?.widthTracksTextView = true
        view.textContainer?.containerSize = NSSize.init(width: 360, height:CGFloat.greatestFiniteMagnitude)
        view.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
        view.minSize = NSSize(width: 0, height: CGFloat.greatestFiniteMagnitude)
        view.isEditable = false
        return view
    }()
    
    lazy var scrollview: NSScrollView = {
        let view = NSScrollView()
        //view.borderType = .noBorder
        view.hasVerticalScroller = true
        view.hasHorizontalScroller = false
        view.documentView = self.contView
        return view
    }()
    
    @objc func copyBtnAction(senber:ZHButton) {
        
        NSPasteboard.general.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
        NSPasteboard.general.setString(self.contView.textStorage!.string, forType: NSPasteboard.PasteboardType.string)
        self.view.makeToast(message: "复制成功".localized(),isImage:true)
    }
}

extension ShareViewController:NSTextViewDelegate {

    
    func textStorage(_ textStorage: NSTextStorage, didProcessEditing editedMask: NSTextStorageEditActions, range editedRange: NSRange, changeInLength delta: Int) {
        self.perform(#selector(self.setHeightToMatchContents), with: nil, afterDelay: 0.0)

    }
    

    func naturalSize()->NSSize {
        let bounds:NSRect = self.contView.bounds;
        let layoutManager:NSLayoutManager = self.contView.textStorage!.layoutManagers[0]
        let textContainer:NSTextContainer = layoutManager.textContainers[0]
        textContainer.containerSize = NSMakeSize(bounds.size.width, 1.0e7)
        layoutManager.glyphRange(for: textContainer)
        let naturalSize: NSSize? = layoutManager.usedRect(for: textContainer).size
        return naturalSize!
    }

    @objc func setHeightToMatchContents() {
        let naturalSize: NSSize = self.naturalSize()
        if let scrollView = self.contView.enclosingScrollView {
            let frame = scrollView.frame
            scrollView.frame = NSMakeRect(frame.origin.x, frame.origin.y, frame.size.width, naturalSize.height + 4)
        }
    }

}
